﻿using CallCenter.Data.Base;
using CallCenter.Data.BizUtilities.Base;
using CallCenter.Data.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CallCenter.Data.BizUtilities
{
    public class MenuModuleDefinition:BaseUtility
    {

        public MenuModuleDefinition(DataManager manager)
            : base(manager)
        { }


        public int Insert(string strDescription, string strPermissionCode, bool bEnabled)
        {

            int nRtn = -1;
string sql = "INSERT INTO MenuModuleDefs " +
                "(Description, PermissionCode, Enabled) " +
                "VALUES(@Description, @PermissionCode, @Enabled)" +
                "SELECT @pResult =@@IDENTITY";


            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@Description", SqlDbType.NVarChar, 50, strDescription);
                this.Factory.AddParameter(cmd, "@PermissionCode", SqlDbType.NVarChar, 50, strPermissionCode);
                this.Factory.AddParameter(cmd, "@Enabled", SqlDbType.Bit, 1, bEnabled);



                var param = this.Factory.AddParameter(cmd, "@pResult", System.Data.SqlDbType.NVarChar, 500, null);
                param.Direction = System.Data.ParameterDirection.Output;

                this.Factory.ExecuteNonQuery(cmd);
                nRtn = Convert.ToInt32(param.Value);
            }
            return nRtn;

        }


        public void Update(int nID, string strDescription, string strPermissionCode, bool bEnabled)
        {

            string sql = "UPDATE MenuModuleDefs " +
                 "SET Description = @Description, " +
                 "PermissionCode = @PermissionCode, " +
                 "Enabled = @Enabled " +
                 "WHERE ID = @ID";



            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@Description", SqlDbType.NVarChar, 50, strDescription);
                this.Factory.AddParameter(cmd, "@PermissionCode", SqlDbType.NVarChar, 50, strPermissionCode);
                this.Factory.AddParameter(cmd, "@Enabled", SqlDbType.Bit, 1, bEnabled);


                this.Factory.AddParameter(cmd, "@ID", SqlDbType.Int, 10, nID);
                this.Factory.ExecuteNonQuery(cmd);
            }
        }

                
    }
}
